
const { static } = require("express");
// 文章数据模型
module.exports = class Resume extends require("./model") {
    /**
     * @param {String} userId 查询简历信息
     */
    static async getResumeInfo(userId, id, from) {
        let resume = {};
        // 查询是否有个人信息数据
        // let sql = `SELECT * FROM personalInfo WHERE 1=1 AND userId = ? AND id = ? ORDER BY userId ASC LIMIT 1`;
        let sql = "SELECT * FROM personalInfo WHERE 1=1 ";
        sql += `AND userId = ${userId} `
        sql += from === 'personalInfo' && id !== undefined ? `AND id = ${id} ` : ''
        sql += `ORDER BY userId ASC LIMIT 1`
        resume.personalinfo = await this.query(sql);
        // console.log( resume.personalinfo ,'============');
        resume.personalinfo = resume.personalinfo[0];
        // 查询是否有社交链接
        // sql = `SELECT * FROM socialLinks WHERE userId = ? ORDER BY userId ASC LIMIT 1`;
        sql = "SELECT * FROM socialLinks WHERE 1=1 ";
        // sql += `AND userId = ${userId} AND id = ${id} ORDER BY userId ASC LIMIT 1`
        sql += `AND userId = ${userId} `
        sql += from === 'socialLinks' && id !== undefined ? `AND id = ${id} ` : ''
        sql += `ORDER BY userId`;
        resume.socialLinks = await this.query(sql);
        // resume.socialLinks = resume.socialLinks[0];
        // 查询是否有技能信息
        // sql = `SELECT * FROM skill WHERE userId = ? ORDER BY userId ASC LIMIT 1`;
        sql = "SELECT * FROM skill WHERE 1=1 ";
        // sql += `AND userId = ${userId} AND id = ${id} ORDER BY userId ASC LIMIT 1`
        sql += `AND userId = ${userId} `
        sql += from === 'skill' && id !== undefined ? `AND id = ${id} ` : ''
        sql += `ORDER BY userId`
        resume.skill = await this.query(sql);
        // resume.skill = resume.skill[0];
        // 查询是否有项目信息
        // sql = `SELECT * FROM project WHERE userId = ? ORDER BY userId ASC LIMIT 1`;
        sql = "SELECT * FROM project WHERE 1=1 ";
        // sql += `AND userId = ${userId} AND id = ${id} ORDER BY userId ASC LIMIT 1`
        sql += `AND userId = ${userId} `
        sql += from === 'project' && id !== undefined ? `AND id = ${id} ` : ''
        sql += `ORDER BY userId`
        resume.project = await this.query(sql);
        // resume.project = resume.project[0];
        // 查询是否有工作信息
        // sql = 'SELECT * FROM `work` WHERE userId = ? ORDER BY userId ASC LIMIT 1';
        sql = "SELECT * FROM work WHERE 1=1 ";
        // sql += `AND userId = ${userId} AND id = ${id} ORDER BY userId ASC LIMIT 1`
        sql += `AND userId = ${userId} `
        sql += from === 'work' && id !== undefined ? `AND id = ${id} ` : ''
        sql += `ORDER BY userId`
        resume.work = await this.query(sql);
        // resume.work = resume.work[0];
        // 查询是否有学历信息
        // sql = `SELECT * FROM education WHERE userId = ? ORDER BY userId ASC LIMIT 1`;
        sql = "SELECT * FROM education WHERE 1=1 ";
        // sql += `AND userId = ${userId} AND id = ${id} ORDER BY userId ASC LIMIT 1`
        sql += `AND userId = ${userId} `
        sql += from === 'education' && id !== undefined ? `AND id = ${id} ` : ''
        sql += `ORDER BY userId`
        resume.education = await this.query(sql);
        // resume.education = resume.education[0];
        return resume;
    }
    static getProject(userId) {
        return new Promise((resolve, reject) => {
            let sql = `SELECT * FROM personalInfo WHERE 1=1 AND userId = ${userId} ORDER BY userId`;
            this.query(sql, [userId])
                .then((res) => {
                    resolve(res);
                })
                .catch((err) => {
                    console.log(`获取指定文章失败：${err.message}`);
                    reject(err);
                });
        });
    }
    /**
     * 
     * @param {Object} personalInfo 个人信息数据
     */
    static updatePersonalInfo(personalInfo) {
        return new Promise((resolve, reject) => {
            // 查询是否个人信息数据
            this.getResumeInfo(personalInfo.userId, personalInfo.id, 'personalInfo').then(res => {
                let RowDataPacket = res.personalinfo;
                if (RowDataPacket && personalInfo.id !== undefined) {
                    console.log(personalInfo);
                    // 编辑
                    let sql = `UPDATE personalInfo SET userImg = ?,userName = ?,address = ?,mailbox = ?,birthday = ?,phone = ?,occupation = ?,introduce = ? WHERE id = ?`
                    const { userImg, userName, address, mailbox, birthday, phone, occupation, introduce, id } = personalInfo;
                    this.query(sql, [userImg, userName, address, mailbox, birthday, phone, occupation, introduce, id]).then(data => {
                        resolve({
                            affectedRows: data.affectedRows,
                            msg: '编辑'
                        });
                    })
                } else {
                    // 新增
                    let sql = "INSERT INTO personalInfo SET ?";
                    this.query(sql, personalInfo).then(data => {
                        // resolve(data.affectedRows);
                        resolve({
                            affectedRows: data.affectedRows,
                            msg: '添加'
                        });
                    })
                }
            })
                .catch((err) => {
                    console.log(`编辑个人信息失败：${err.message}`);
                    reject(err);
                });
        });
    }
    /**
     * 
     * @param {Object} linkInfo 社交链接
     */
    static updateSocialLinks(linkInfo) {
        return new Promise((resolve, reject) => {
            // 查询是否社交信息数据
            linkInfo.forEach(item => {
                this.getResumeInfo(item.userId, item.id, 'socialLinks').then(res => {
                    let RowDataPacket = res.socialLinks;
                    if (RowDataPacket.length > 0 && item.id !== undefined) {
                        // 编辑
                        let sql = `UPDATE socialLinks SET linkImg = ?,linkName = ?,onlinelink = ? WHERE id = ?`
                        const { linkImg, linkName, onlinelink, id } = item
                        this.query(sql, [linkImg, linkName, onlinelink, id]).then(data => {
                            resolve({
                                affectedRows: data.affectedRows,
                                msg: '编辑'
                            });
                        })
                    } else {
                        // 新增
                        let sql = "INSERT INTO socialLinks SET ?";
                        this.query(sql, item).then(data => {
                            resolve({
                                affectedRows: data.affectedRows,
                                msg: '添加'
                            });
                        })
                    }
                }).catch((err) => {
                    console.log(`编辑社交信息失败：${err.message}`);
                    reject(err);
                });
            });

        })

    }
    /**
     * 
     * @param {Object} skillInfo 技能链接
     */
    static updateSkill(skillInfo) {
        return new Promise((resolve, reject) => {
            // 查询是否技能链接数据
            skillInfo.forEach(item => {
                this.getResumeInfo(item.userId, item.id, 'skill').then(res => {
                    let RowDataPacket = res.skill;
                    if (RowDataPacket.length > 0 && item.id !== undefined) {
                        // 编辑
                        let sql = `UPDATE skill SET skillName = ?,percentage = ? WHERE id = ?`
                        const { skillName, percentage, id } = item;
                        this.query(sql, [skillName, percentage, id]).then(data => {
                            resolve({
                                affectedRows: data.affectedRows,
                                msg: '编辑'
                            });
                        })
                    } else {
                        // 新增
                        let sql = "INSERT INTO skill SET ?";
                        this.query(sql, item).then(data => {
                            resolve({
                                affectedRows: data.affectedRows,
                                msg: '添加'
                            });
                        })
                    }
                }).catch((err) => {
                    console.log(`编辑技能链接失败：${err.message}`);
                    reject(err);
                });
            })

        });
    }
    /**
     * 
     * @param {Object} projectInfo 项目
     */
    static updateProject(projectInfo) {
        return new Promise((resolve, reject) => {
            // 查询是否项目数据
            projectInfo.forEach(item => {
                this.getResumeInfo(item.userId, item.id, 'project').then(res => {
                    let RowDataPacket = res.project;
                    if (RowDataPacket.length > 0 && item.id !== undefined) {
                        // 编辑
                        let sql = `UPDATE project SET projectName = ?,projectTime = ?,projectRole = ?,projectContent = ?,projectImg = ?,projectLink = ? WHERE id = ?`
                        const { projectName, projectTime, projectRole, projectContent, projectImg, projectLink, id } = item;
                        this.query(sql, [projectName, projectTime, projectRole, projectContent, projectImg, projectLink, id]).then(data => {
                            resolve({
                                affectedRows: data.affectedRows,
                                msg: '编辑'
                            });
                        })
                    } else {
                        // 新增
                        let sql = "INSERT INTO project SET ?";
                        this.query(sql, item).then(data => {
                            resolve({
                                affectedRows: data.affectedRows,
                                msg: '添加'
                            });
                        })
                    }
                }).catch((err) => {
                    console.log(`编辑项目失败：${err.message}`);
                    reject(err);
                });
            })
        });
    }
    /**
     * 
     * @param {Object} workInfo 工作信息
     */
    static updateWork(workInfo) {
        return new Promise((resolve, reject) => {
            // 查询是否工作信息数据
            workInfo.forEach(item => {
                this.getResumeInfo(item.userId, item.id, 'work').then(res => {
                    let RowDataPacket = res.work;
                    if (RowDataPacket.length > 0 && item.id !== undefined) {
                        // 编辑
                        let sql = `UPDATE work SET corporateName = ?,workingTime = ?,department = ?,position = ?,jobContent = ? WHERE id = ?`
                        const { corporateName, workingTime, department, position, jobContent, id } = item
                        this.query(sql, [corporateName, workingTime, department, position, jobContent, id]).then(data => {
                            resolve({
                                affectedRows: data.affectedRows,
                                msg: '编辑'
                            });
                        })
                    } else {
                        // 新增
                        let sql = "INSERT INTO work SET ?";
                        this.query(sql, item).then(data => {
                            resolve({
                                affectedRows: data.affectedRows,
                                msg: '添加'
                            });
                        })
                    }
                }).catch((err) => {
                    console.log(`编辑工作信息失败：${err.message}`);
                    reject(err);
                });
            })
        });
    }
    /**
     * 
     * @param {Object} educationInfo 学历信息
     */
    static updateEducation(educationInfo) {
        return new Promise((resolve, reject) => {
            // 查询是否学历信息数据
            educationInfo.forEach(item => {
                this.getResumeInfo(item.userId, item.id, 'education').then(res => {
                    let RowDataPacket = res.education;
                    if (RowDataPacket.length > 0 && item.id !== undefined) {
                        // 编辑
                        let sql = `UPDATE education SET schoolName = ?,education = ?,major = ?,content = ?,time = ? WHERE id = ?`
                        const { schoolName, education, major, content, time, id } = item;
                        this.query(sql, [schoolName, education, major, content, time, id]).then(data => {
                            resolve({
                                affectedRows: data.affectedRows,
                                msg: '编辑'
                            });
                        })
                    } else {
                        // 新增
                        let sql = "INSERT INTO education SET ?";
                        this.query(sql, item).then(data => {
                            resolve({
                                affectedRows: data.affectedRows,
                                msg: '添加'
                            });
                        })
                    }
                }).catch((err) => {
                    console.log(`编辑工作信息失败：${err.message}`);
                    reject(err);
                });
            })
        });
    }
    /**
  * 删除文章
  * @param {integer} id 文章Id
  */
    static del(enumeration, id) {
        return new Promise((resolve, reject) => {
            let sql = "DELETE FROM " + enumeration + " WHERE id = ?";
            this.query(sql, id)
                .then((res) => {
                    resolve(res.affectedRows);
                })
                .catch((err) => {
                    console.log(`删除失败：${err.message}`);
                    reject(err);
                });
        });
    }
}

